home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
src
/
haeberli
/
libgutil
/
bufpool.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
4KB
|
142 lines
/*
* Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
* the contents of this file may not be disclosed to third parties, copied or
* duplicated in any form, in whole or in part, without the prior written
* permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to restrictions
* as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
* and Computer Software clause at DFARS 252.227-7013, and/or in similar or
* successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
* rights reserved under the Copyright Laws of the United States.
*/
/*
* bufpool.c
*
* $Revision: 1.2 $
*/
#include "stdio.h"
#include "assert.h"
#include "bufpool.h"
/* local functions */
static void grow_pool( register Pool * );
/*-----------------------------------------------------------------------------
* new_pool - allocate a new pool of buffers
*-----------------------------------------------------------------------------
*/
Pool *
new_pool( int buffersize, int initpoolsize, char *name )
{
register Pool *p;
p = (Pool *) mymalloc( sizeof (Pool) );
p->buffersize= (buffersize < sizeof(Buffer)) ? sizeof(Buffer)
: buffersize;
p->nextsize = initpoolsize * p->buffersize;
#ifndef NDEBUG
p->name = name;
p->magic = is_allocated;
#endif
p->nextblock= 0;
p->curblock = 0;
p->freelist = 0;
p->nextfree = 0;
return p;
}
/*-----------------------------------------------------------------------------
* new_buffer - allocate a buffer from a pool
*-----------------------------------------------------------------------------
*/
char *
new_buffer( register Pool *p )
{
char *buffer;
assert( p && (p->magic == is_allocated) );
/* find free buffer */
if( p->freelist ) {
buffer = (char *) p->freelist;
p->freelist = p->freelist->next;
} else {
if( ! p->nextfree )
grow_pool( p );
p->nextfree -= p->buffersize;;
buffer = p->curblock + p->nextfree;
}
return buffer;
}
static void
grow_pool( register Pool *p )
{
assert( p && (p->magic == is_allocated) );
p->curblock = (char *) mymalloc( p->nextsize );
p->blocklist[p->nextblock++] = p->curblock;
p->nextfree = p->nextsize;
p->nextsize *= 2;
}
/*-----------------------------------------------------------------------------
* free_buffer - return a buffer to a pool
*-----------------------------------------------------------------------------
*/
void
free_buffer( Pool *p, void *b )
{
assert( p && (p->magic == is_allocated) );
/* add buffer to singly connected free list */
((Buffer *) b)->next = p->freelist;
p->freelist = (Buffer *) b;
}
/*-----------------------------------------------------------------------------
* free_pool - free a pool of buffers and the pool itself
*-----------------------------------------------------------------------------
*/
void
free_pool( Pool *p )
{
assert( p && (p->magic == is_allocated) );
while( p->nextblock )
free( p->blocklist[--(p->nextblock)] );
#ifndef NDEBUG
p->magic = is_free;
#endif
free( p );
}
/*-----------------------------------------------------------------------------
* clear_pool - free buffers associated with pool but keep pool
*-----------------------------------------------------------------------------
*/
void
clear_pool( Pool *p )
{
assert( p && (p->magic == is_allocated) );
while( p->nextblock )
free( p->blocklist[--(p->nextblock)] );
p->curblock = 0;
p->freelist = 0;
p->nextfree = 0;
if( p->nextsize >= 2 * p->buffersize )
p->nextsize /= 2;
}